package com.ww.grtsmanager.service.impl;

import com.ww.grtsmanager.constant.EduConstant;
import com.ww.grtsmanager.bean.Code;
import com.ww.grtsmanager.bean.User;
import com.ww.grtsmanager.mapper.CodeMapper;
import com.ww.grtsmanager.mapper.UserMapper;
import com.ww.grtsmanager.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CodeMapper codeMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public User login(String userName, String passWord) {
        if (StringUtils.isBlank(userName)){
            throw new RuntimeException("账号不能为空");
        }
        if (StringUtils.isBlank(passWord)){
            throw new RuntimeException("密码不能为空");
        }
        User user = userMapper.getUserByUserName(userName);
        if (user == null){
            throw new RuntimeException("用户名有误");
        }
        //与加密后的密码进行比较
        if (!user.getPassWord().equals(DigestUtils.md5Hex(passWord))){
        throw new RuntimeException("密码有误");
        }
        //不把加密以后的密码传到页面
        user.setPassWord(null);
        return user;
    }

    @Override
    public void register(User user) {
        user.setIsDelete(true);
        Date date = new Date();
        user.setGmtCreated(date);
        user.setGmtModified(date);
        user.setPassWord(DigestUtils.md5Hex(user.getPassWord()));
        int i = userMapper.addUser(user);
        if (i == 0){
            throw new RuntimeException("注册用户失败");
        }
    }

    @Override
    public void checked(String data, int type) {
        if (type == 1){
            User user = userMapper.getUserByUserName(data);
            if (user!=null){
                throw new RuntimeException("用户名已经存在");
            }
        } else if (type == 2) {
            User user = userMapper.getUserByEmail(data);
            if (user!=null){
                throw new RuntimeException("邮箱已经存在");
            }
        } else if (type==3) {
            User user = userMapper.getUserByEmail(data);
            if (user == null){
                throw new RuntimeException("邮箱不存在请重新输入");
            }
        }
    }

    @Override
    public void generateCode(String email) {
        Random randObj = new Random();
        int code =100000+randObj.nextInt(900000);
        Code codeObj = new Code();
        codeObj.setCode(code);
        codeObj.setEmail(email);
        codeObj.setGmtCreated(new Date());
        //把验证码保存到数据库中
        int i = codeMapper.addCode(codeObj);
        if (i==0){
            throw new RuntimeException("生成验证码失败，请重试！");
        }
        //准备开始发送邮件
        SimpleMailMessage simpMsg = new SimpleMailMessage();
        simpMsg.setFrom(EduConstant.SENDER);
        //（收件人）页面传递过来的邮箱
        simpMsg.setTo(email);
        simpMsg.setSubject("grts安全中心-找回密码");
        simpMsg.setText("尊敬的用户您好：您正在进行找回密码操作，验证码为"+code+"，本验证码5分钟内有效");
        javaMailSender.send(simpMsg);
    }

    @Override
    public void changePassWord(String email, Integer code, String passWord) {
        if (code==null){
            throw new RuntimeException("验证码不能为空");
        }
        if (StringUtils.isBlank(passWord)){
            throw new RuntimeException("密码不能为空");
        }
        //根据邮箱查询时间
        Date startTime = codeMapper.getCodeByEmail(email,code);
        if (startTime==null){
            throw new RuntimeException("验证码有误，请重新发送");
        }
        Date endTime = new Date();
        long l = endTime.getTime()- startTime.getTime();
        if ((l/1000/60)>5){
            throw new RuntimeException("验证码过期，请重新发送");
        }
        boolean matches = passWord.matches("[a-zA-Z][a-zA-Z0-9]{5,12}");
        if (!matches){
            throw new RuntimeException("您输入的密码不符合规则");
        }
        //修改密码
       int i = userMapper.updateUserByEmail(DigestUtils.md5Hex(passWord),email,new Date());
        if (i == 0){
            throw new RuntimeException("修改密码失败");
        }
    }
}
